package com.smedi.ismedi.cronjob.api.infrastructure.cron;

import com.smedi.ismedi.cronjob.api.infrastructure.enumerate.CronTypeEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class CronType {
    private CronTypeEnum type;

    private String[] values;

    public static CronType builder(int value) {
        CronType cronType = new CronType();
        cronType.setType(CronTypeEnum.AGGREGATE);
        cronType.setValues(new String[]{String.valueOf(value)});
        return cronType;
    }

    public static CronType builder(CronTypeEnum type, String[] values) {
        CronType cronType = new CronType();
        cronType.setType(type);
        cronType.setValues(values);
        return cronType;
    }

    @Override
    public String toString() {
        switch (type) {
            case AGGREGATE:
                return StringUtils.join(values, ",");
            case SECTION:
                return StringUtils.join(values, "-");
            case INTERVAL:
                return StringUtils.join(values, "/");
            case EVERY_TIME:
                return "*";
            case LAST:
                return "L";
            case NONE:
                return "?";
        }
        return "*";
    }
}
